package com.chuanyang.driverh.mainmy.fragment;


import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.mainmy.HisWayBillDetailActivity;
import com.chuanyang.driverh.responseresult.WayBillHistryData;
import com.chuanyang.driverh.responseresult.WayBillHistryResult;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.TimeUtils;
import com.chuanyang.driverh.utils.eventbus.RefleshFragEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class WaybillHisFragment extends BaseListFragment {
    private HashMap params;

    private OrderHistoryAdapter adaper;

    @Override
    public BaseListAdapter createAdapter() {
        return adaper = new OrderHistoryAdapter(getActivity());
    }



    @Override
    protected void initListener() {

    }

    @Override
    public void initView(View view) {
        super.initView(view);
    }

    @Override
    protected void initData() {

    }

    public void onEventMainThread(RefleshFragEvent event) {
        if (event.getIndex() == 7) {
            mCurrentPage = 1;
            getData();
        }


    }


    private void getData() {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("page", mCurrentPage + "");
        params.put("length", "10");
        MyHttpUtil.historyWayBill(getActivity(),params, new MyCallback<WayBillHistryResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                springView.onFinishFreshAndLoad();
                onShowFailed();
            }
            @Override
            public void onSuccess(WayBillHistryResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    setDataResult(result.getData().getData());
                }else{
                    onShowNull();
                    springView.onFinishFreshAndLoad();
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });



    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {

        WayBillHistryData.DataBean bean2 = (WayBillHistryData.DataBean) adaper.getItem(position);
        Intent intent = new Intent(getActivity(), HisWayBillDetailActivity.class);
        intent.putExtra("WaybillId", bean2.getWaybillId() + "");
        startActivity(intent);

    }


    @Override
    protected void onForceRefresh() {
        super.onForceRefresh();
        mCurrentPage = 1;
        getData();
    }

    @Override
    public void onGetMore() {
        getData();
    }

    @Override
    public void onRefresh() {
        mCurrentPage = 1;
        getData();
    }

    @Override
    protected boolean isPullToRefresh() {
        return true;
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }

    private class OrderHistoryAdapter extends BaseListAdapter {

        private ViewHolder viewHolder;

        public OrderHistoryAdapter(Context context) {
            super(context);
        }

        @Override
        public View getZkView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getInflater().inflate(R.layout.my_item_historyorder, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.iv_orderhis_image = (ImageView) convertView.findViewById(R.id.iv_orderhis_image);
                viewHolder.iv_orderhis_fleedpersonal = (ImageView) convertView.findViewById(R.id.iv_orderhis_fleedpersonal);//车队还是个人
                viewHolder.tv_orderhis_name = (TextView) convertView.findViewById(R.id.tv_orderhis_name);
                viewHolder.tv_orderhis_startadd = (TextView) convertView.findViewById(R.id.tv_orderhis_startadd);
                viewHolder.tv_orderhis_endadd = (TextView) convertView.findViewById(R.id.tv_orderhis_endadd);
                viewHolder.tv_orderhis_starttim = (TextView) convertView.findViewById(R.id.tv_orderhis_starttim);
                viewHolder.tv_orderhis_endtim = (TextView) convertView.findViewById(R.id.tv_orderhis_endtim);
                viewHolder.tv_orderhis_carshipstarttim = (TextView) convertView.findViewById(R.id.tv_orderhis_carshipstarttim);
                viewHolder.tv_orderhis_carshipendtim = (TextView) convertView.findViewById(R.id.tv_orderhis_carshipendtim);
                viewHolder.tv_orderhis_amount = (TextView) convertView.findViewById(R.id.tv_orderhis_amount);
                viewHolder.tv_orderhis_taskname = (TextView) convertView.findViewById(R.id.tv_orderhis_taskname);
                viewHolder.tv_orderhis_orialweight = (TextView) convertView.findViewById(R.id.tv_orderhis_orialweight);
                viewHolder.tv_orderhis_type = (TextView) convertView.findViewById(R.id.tv_orderhis_type);
                viewHolder.tv_orderhis_tradetype = (TextView) convertView.findViewById(R.id.tv_orderhis_tradetype);
                // viewHolder.tv_orderhis_share= (TextView) convertView.findViewById(R.id.tv_orderhis_share);
                viewHolder.rb_stars = (RatingBar) convertView.findViewById(R.id.rb_stars);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();

            }

            WayBillHistryData.DataBean bean2 = (WayBillHistryData.DataBean) getItem(position);
            BitmapUtil.showImgForNetPath(bean2.getUserImage(), viewHolder.iv_orderhis_image);
            viewHolder.iv_orderhis_fleedpersonal.setImageResource(R.mipmap.individual);
            viewHolder.tv_orderhis_name.setText(bean2.getUsername());
            viewHolder.tv_orderhis_startadd.setText(bean2.getStartAddr());
            viewHolder.tv_orderhis_endadd.setText(bean2.getTargetAddr());
            viewHolder.rb_stars.setRating(bean2.getStar());
            if (bean2.getCurrency() == 2)//1内贸 2外贸
            {
                viewHolder.tv_orderhis_tradetype.setVisibility(View.VISIBLE);
            } else {
                viewHolder.tv_orderhis_tradetype.setVisibility(View.GONE);

            }
            if (bean2.getTransPortWay() == 1)//车
            {
                viewHolder.tv_orderhis_starttim.setText(TimeUtils.getStandardTime(bean2.getOrder_startime()));
                viewHolder.tv_orderhis_endtim.setText(TimeUtils.getStandardTime(bean2.getOrder_endtime()));
                viewHolder.tv_orderhis_carshipstarttim.setText("接货");
                viewHolder.tv_orderhis_carshipendtim.setText("卸货");
                viewHolder.iv_orderhis_fleedpersonal.setImageResource(R.mipmap.car_transport1);

            } else if (bean2.getTransPortWay() == 2)//船
            {
                viewHolder.tv_orderhis_starttim.setText(bean2.getLoadDate());
                viewHolder.tv_orderhis_endtim.setText(bean2.getStevedoreDay());
                viewHolder.tv_orderhis_carshipstarttim.setText("受载日期");
                viewHolder.tv_orderhis_carshipendtim.setText("装卸期限");
                viewHolder.iv_orderhis_fleedpersonal.setImageResource(R.mipmap.shipping1);

            }
            if (bean2.getAmount() != null) {
                viewHolder.tv_orderhis_amount.setText(bean2.getAmount() + bean2.getUnit());

            } else {
                viewHolder.tv_orderhis_amount.setText("---");

            }
            if(TextUtils.isEmpty(bean2.getAssignment()))
            {
                viewHolder.tv_orderhis_taskname.setText("");
            }
            else {
                viewHolder.tv_orderhis_taskname.setText(bean2.getAssignment());

            }
            if(bean2.getRealitySum()==null)
            {
                viewHolder.tv_orderhis_orialweight.setText("");
            }
            else {
                viewHolder.tv_orderhis_orialweight.setText(bean2.getRealitySum().getRealityWeight()+"t");

            }
            //根据订单类型确定显示
            if (bean2.getOrder_type() == 1)//竞价
            {
                viewHolder.tv_orderhis_type.setText("竞价");
                viewHolder.tv_orderhis_tradetype.setBackgroundColor(getResources().getColor(R.color.compete));

                viewHolder.tv_orderhis_type.setBackgroundResource(R.drawable.tv_shape_order_compete);

            } else if (bean2.getOrder_type() == 2)//匹配
            {
                viewHolder.tv_orderhis_type.setText("匹配");
                viewHolder.tv_orderhis_tradetype.setBackgroundColor(getResources().getColor(R.color.match));

                viewHolder.tv_orderhis_type.setBackgroundResource(R.drawable.tv_shape_order_macher);


            } else {//专线
                viewHolder.tv_orderhis_type.setText("专线");
                viewHolder.tv_orderhis_tradetype.setBackgroundColor(getResources().getColor(R.color.specialline));
                viewHolder.tv_orderhis_type.setBackgroundResource(R.drawable.tv_shape_order_special);

            }

            return convertView;
        }

        private class ViewHolder {
            TextView tv_orderhis_name;
            TextView tv_orderhis_startadd;
            TextView tv_orderhis_endadd;
            TextView tv_orderhis_starttim;
            TextView tv_orderhis_endtim;
            TextView tv_orderhis_carshipstarttim;
            TextView tv_orderhis_carshipendtim;
            TextView tv_orderhis_taskname;//任务名称
            TextView tv_orderhis_orialweight;//实际重量
            RatingBar rb_stars;
            TextView tv_orderhis_amount;
            TextView tv_orderhis_share;
            TextView tv_orderhis_type;
            TextView tv_orderhis_tradetype;
            ImageView iv_orderhis_image;
            ImageView iv_orderhis_fleedpersonal;
        }
    }

}
